package com.cskaoyan.service;

import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.bean.*;
import com.cskaoyan.bo.SearchGrouponRulesBO;
import com.cskaoyan.bo.AdBO;
import com.cskaoyan.bo.CouponBO;
import com.cskaoyan.bo.CouponUserBo;
import com.cskaoyan.bo.SearchTopicBO;
import com.cskaoyan.mapper.*;
import com.cskaoyan.vo.*;

import com.cskaoyan.vo.AdVO;
import com.cskaoyan.vo.CouponUserVO;
import com.cskaoyan.vo.CouponVO;
import com.cskaoyan.vo.TopicVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ExtensionManagementServiceImpl implements ExtensionManagementService {
    @Autowired
    private AdMapper adMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponUserMapper couponUserMapper;

    @Autowired
    private GrouponRulesMapper grouponRulesMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GrouponRecordMapper grouponRecordMapper;

    /**
     * 广告列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @Override
    public AdVO query(AdBO adBO) {
        //分页
        PageHelper.startPage(adBO.getPage(),adBO.getLimit());
        //排序
        AdExample adExample = new AdExample();
        adExample.setOrderByClause(adBO.getSort()+" "+ adBO.getOrder());
        AdExample.Criteria criteria = adExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(adBO.getName())){
            criteria.andNameLike("%"+adBO.getName()+"%");
        }
        if (Objects.nonNull(adBO.getContent())){
            criteria.andContentLike("%"+adBO.getContent()+"%");
        }
        List<Ad> ads = adMapper.selectByExample(adExample);
        //总数
        PageInfo<Ad> adPageInfo = new PageInfo<>(ads);
        long total = adPageInfo.getTotal();

        AdVO adVO = new AdVO();
        adVO.setItems(ads);
        adVO.setTotal(total);
        return adVO;
    }


    /**
     * 获取专题列表
     * @param searchTopicBO
     * @return
     */
    @Override
    public TopicVO getTopic(SearchTopicBO searchTopicBO){
        //分页
        PageHelper.startPage(searchTopicBO.getPage(), searchTopicBO.getLimit());

        TopicExample topicExample = new TopicExample();

        //搜索结果排序
        topicExample.setOrderByClause(searchTopicBO.getSort() + " " + searchTopicBO.getOrder());

        //添加搜索条件
        TopicExample.Criteria criteria = topicExample.createCriteria();
        //判断是否已逻辑删除 deleted:true
        criteria.andDeletedNotEqualTo(true);
        if(Objects.nonNull(searchTopicBO.getTitle())){
            criteria.andTitleLike("%" + searchTopicBO.getTitle() + "%");
        }
        if(Objects.nonNull(searchTopicBO.getSubtitle())){
            criteria.andSubtitleLike("%" + searchTopicBO.getSubtitle() + "%");
        }

        //查询
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();

        TopicVO topicVO = new TopicVO();
        topicVO.setItems(topics);
        topicVO.setTotal(total);
        return topicVO;
    }

    /**
     * 增加专题
     * @param topic
     * @return
     */
    @Override
    public CreateTopicVO createTopic(Topic topic) {
        Date date = new Date();
        topic.setAddTime(date);
        topic.setUpdateTime(date);

        //返回id
        int count = topicMapper.insertSelective(topic);

        CreateTopicVO createTopicVO = new CreateTopicVO(topic.getPicUrl(), topic.getAddTime(), topic.getPrice(),
                topic.getSubtitle(), topic.getGoods(), topic.getUpdateTime(), topic.getId(), topic.getTitle(),
                topic.getReadCount(), topic.getContent());
        return createTopicVO;
    }

    /**
     * 删除专题
     * @param topic
     */
    @Override
    public void deleteTopic(Topic topic) {
        Date date = new Date();
        topic.setDeleted(true);
        topic.setUpdateTime(date);
        int count = topicMapper.updateByPrimaryKey(topic);
    }

    /**
     * 编辑专题
     * @param topic
     */
    @Override
    public void updateTopic(Topic topic) {
        topic.setUpdateTime(new Date());
        int count = topicMapper.updateByPrimaryKey(topic);
    }

    /**
     * 编辑广告
     * @param ad
     */
    @Override
    public void updateAd(Ad ad) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(date);
        ad.setUpdateTime(date);
        adMapper.updateByPrimaryKeySelective(ad);
    }

    /**
     * 添加广告
     * @param ad
     */
    @Override
    public Ad addAd(Ad ad) {
        ad.setId(null);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(date);
        ad.setAddTime(date);
        ad.setStartTime(date);
        ad.setDeleted(false);
        adMapper.insertSelective(ad);

        Ad ad1 = adMapper.selectByPrimaryKey(ad.getId());
        return ad1;
    }

    /**
     * 删除广告
     * @param ad
     */
    @Override
    public void deleteAd(Ad ad) {
        ad.setDeleted(true);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(date);
        ad.setEndTime(date);
        adMapper.updateByPrimaryKeySelective(ad);
    }

    /**
     * 优惠券列表
     * @return
     */
    @Override
    public CouponVO queryCoupon(CouponBO couponBO) {
        PageHelper.startPage(couponBO.getPage(),couponBO.getLimit());
        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause(couponBO.getSort()+" "+couponBO.getOrder());
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(couponBO.getName())){
            criteria.andNameLike("%"+couponBO.getName()+"%");
        }
        if (Objects.nonNull(couponBO.getType())){
            criteria.andTypeEqualTo(couponBO.getType());
        }
        if (Objects.nonNull(couponBO.getStatus())){
            criteria.andStatusEqualTo(couponBO.getStatus());
        }
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();
        CouponVO couponVO = new CouponVO();
        couponVO.setItems(coupons);
        couponVO.setTotal(total);
        return couponVO;
    }

    /**
     * 删除优惠券
     * @param coupon
     */
    @Override
    public void deleteCoupon(Coupon coupon) {
        coupon.setDeleted(true);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(date);
        coupon.setEndTime(date);
        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    /**
     * 添加优惠卷
     * @param coupon
     */
    @Override
    public Coupon addCoupon(Coupon coupon) {
        coupon.setId(null);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(date);
        coupon.setAddTime(date);
        coupon.setStartTime(date);
        coupon.setDeleted(false);
        couponMapper.insertSelective(coupon);

        Coupon coupon1 = couponMapper.selectByPrimaryKey(coupon.getId());
        return coupon1;
    }

    /**
     * 编辑优惠券
     * @param coupon
     */
    @Override
    public void updateCoupon(Coupon coupon) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(date);
        coupon.setUpdateTime(date);
        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    /**
     * 优惠券详情
     * @param id
     */
    @Override
    public Coupon getCoupon(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    /**
     * 优惠券用户列表
     * @return
     */
    @Override
    public CouponUserVO queryCouponUser(CouponUserBo couponUserBo) {
        PageHelper.startPage(couponUserBo.getPage(),couponUserBo.getLimit());
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.setOrderByClause(couponUserBo.getSort()+" "+couponUserBo.getOrder());
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(couponUserBo.getUserId())){
            criteria.andUserIdEqualTo(couponUserBo.getUserId());
        }
        if (Objects.nonNull(couponUserBo.getStatus())) {
            criteria.andStatusEqualTo(couponUserBo.getStatus());
        }
        if (Objects.nonNull(couponUserBo.getCouponId())){
            criteria.andCouponIdEqualTo(couponUserBo.getCouponId());
        }
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        PageInfo<CouponUser> couponPageInfo = new PageInfo<>(couponUsers);
        long total = couponPageInfo.getTotal();
        CouponUserVO couponUserVO = new CouponUserVO();
        couponUserVO.setItems(couponUsers);
        couponUserVO.setTotal(total);
        return couponUserVO;
    }

    /**
     *获取团购规则列表
     * @param searchGrouponRulesBO
     * @return
     */
    @Override
    public GrouponRulesVO getGrouponRules(SearchGrouponRulesBO searchGrouponRulesBO) {
        //分页
        PageHelper.startPage(searchGrouponRulesBO.getPage(), searchGrouponRulesBO.getLimit());

        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();

        //搜索结果排序
        grouponRulesExample.setOrderByClause(searchGrouponRulesBO.getSort() + " " + searchGrouponRulesBO.getOrder());

        //判断是否已逻辑删除
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        //根据商品id查询
        if(Objects.nonNull((searchGrouponRulesBO.getGoodsId()))){
            criteria.andGoodsIdLike("%" + searchGrouponRulesBO.getGoodsId() + "%");
        }

        //查询
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        long total = grouponRulesPageInfo.getTotal();

        GrouponRulesVO grouponRulesVO = new GrouponRulesVO();
        grouponRulesVO.setItems(grouponRules);
        grouponRulesVO.setTotal(total);
        return grouponRulesVO;
    }

    /**
     * 删除团购规则
     * @param grouponRules
     */
    @Override
    public void deleteGrouponRules(GrouponRules grouponRules) {
        grouponRules.setDeleted(true);
        int count = grouponRulesMapper.updateByPrimaryKey(grouponRules);
    }

    /**
     * 新增团购规则
     * @param grouponRules
     * @return
     */
    @Override
    public GrouponRules createGrouponRules(GrouponRules grouponRules) {
        Date date = new Date();
        grouponRules.setAddTime(date);
        grouponRules.setUpdateTime(date);

        //根据goodsId查询name和picUrl
        String goodsName = goodsMapper.selectGoodsNameById(grouponRules.getGoodsId());
        String picUrl = goodsMapper.selectPicUrlById(grouponRules.getGoodsId());

        grouponRules.setDeleted(false);
        grouponRules.setGoodsName(goodsName);
        grouponRules.setPicUrl(picUrl);

        //返回id
        int count = grouponRulesMapper.insertSelective(grouponRules);

        return grouponRules;
    }

    /**
     * 编辑团购规则
     * @param grouponRules
     */
    @Override
    public void updateGrouponRules(GrouponRules grouponRules) {
        grouponRules.setUpdateTime(new Date());
        int count = grouponRulesMapper.updateByPrimaryKey(grouponRules);
    }

    /**
     * 获取团购活动列表（复用SearchGrouponRulesBO）
     * @param searchGrouponRulesBO
     * @return
     */
    @Override
    public GrouponRecordVO getGrouponRecord(SearchGrouponRulesBO searchGrouponRulesBO) {
        //分页
        PageHelper.startPage(searchGrouponRulesBO.getPage(), searchGrouponRulesBO.getLimit());

        GrouponRecordExample grouponRecordExample = new GrouponRecordExample();

        //搜索结果排序
        grouponRecordExample.setOrderByClause(searchGrouponRulesBO.getSort() + " " + searchGrouponRulesBO.getOrder());

        //判断是否已逻辑删除
        GrouponRecordExample.Criteria criteria = grouponRecordExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        //根据商品id查询
        if(Objects.nonNull((searchGrouponRulesBO.getGoodsId()))){
            criteria.andGoodsIdLike("%" + searchGrouponRulesBO.getGoodsId() + "%");
        }

        //查询
        List<GrouponRecord> grouponRecords = grouponRecordMapper.selectByExample(grouponRecordExample);
        PageInfo<GrouponRecord> grouponRecordsPageInfo = new PageInfo<>(grouponRecords);
        long total = grouponRecordsPageInfo.getTotal();

        GrouponRecordVO grouponRecordVO = new GrouponRecordVO();
        grouponRecordVO.setItems(grouponRecords);
        grouponRecordVO.setTotal(total);
        return grouponRecordVO;
    }


    /**
     * 判断商品Id是否存在
     * @param grouponRules
     * @return
     */
    @Override
    public Integer exitGoodsId(GrouponRules grouponRules) {
        Integer count = goodsMapper.exitGoodsId(grouponRules.getGoodsId());
        return count;
    }
}
